package database;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

/**
 * class DbFolderManager - provide API of queries of the folders table
 */
public  class   DbFolderManager {
	
	private Session session;
	
	/**
	 * constructor
	 * @param session - a session instance
	 */
	public DbFolderManager(Session session) {
		this.session = session;
	}
	
	/**
	 * save folder in folders table in the DB
	 * @param folder - a folder to add to DB.
	 */
	public synchronized void saveDbFolder(DbFolder folder){
		//System.out.println("save folder");
		session.save(folder);
		session.flush();
		session.clear();
	}
	
	/**
	 * @param userID - user id
	 * @param folderID - folder id
	 * @return the folder from the DB
	 */
	public synchronized DbFolder getDbFolder(String userID, Integer folderID){
		Query query = session.createQuery("from DbFolder");
		
		List<DbFolder> list = query.list();
		Iterator<DbFolder> it=list.iterator();
		
		DbFolder currentFolder= null;
		
		while (it.hasNext()){
			currentFolder=it.next();
			
			if ((userID.equals(currentFolder.getUserID())) && (folderID.equals(currentFolder.getFolderID())))
				return currentFolder;
		}
		
		return null;
	}
	
	
	//the folder from the DB according to userID and folderName
	private DbFolder getDbFolderHelper(String userID, String folderName){
		Query query = session.createQuery("from DbFolder where isDropbox=false");
		
		System.out.println("folder query "+userID + " "+ folderName);
		
		if ((userID==null)||(folderName==null))
			return null;
		
		List<DbFolder> list = query.list();
		Iterator<DbFolder> it=list.iterator();
		
		DbFolder currentFolder= null;
		
		while (it.hasNext()){
			currentFolder=it.next();
			if ((userID.equals(currentFolder.getUserID())) && (folderName.equals(currentFolder.getFolderName())))
			{
				return currentFolder;
			}
		}
		
		return null;
	}
	
	/**
	 * @param userID - user id
	 * @param folderName - folder name
	 * @return the folder from the DB
	 */
	public synchronized DbFolder getDbFolder(String userID, String folderName){
		return getDbFolderHelper(userID, folderName);
	}
	
	/**
	 * update the folder in the DB
	 * @param folder - a folder
	 */
	public synchronized void updateDbFolder(DbFolder folder){
		session.update(folder);
		session.flush();
		session.clear();
	}
	
	/**
	 * delete the folder from the DB
	 * @param folder - a folder
	 * @return 1 if the folder was a shared folder, else return 0.
	 */
	public synchronized int deleteDbFolder(DbFolder folder) {
		session.delete(folder);
		session.flush();
		session.clear();
		
		String queryString = "from DbFolder where folderID='"+ folder.getFolderID() + "'";
		Query query = session.createQuery(queryString);
		List<DbFolder> list = query.list();
		
		if (!list.isEmpty())
			return 1;
		else
			return 0;
		
	}
	
	/**
	 * add new folder to the DB
	 * @param userID - the user id
	 * @param folderName - the folder name
	 * @param isDropbox - 1 if the folder is a root of dropbox, else 0
	 * @param folderIdRequired - if null give the folder new id, else give it that number as an id 
	 * @return if succeed 1, else 0
	 */
	public synchronized int addNewDbFolder(String userID,String folderName,Boolean isDropbox,Integer folderIdRequired){

		Integer folderID;
		DbFolder folder;

		if (getDbFolderHelper(userID, folderName) !=null)
		{
			System.out.println("user already have a folder with that name");
			return 0;
		}			
		
		//give it the next free folderID
		if (folderIdRequired==null)
		{
			Query query = session.createQuery("select max(folderID) from DbFolder");
			folderID = (Integer) query.list().iterator().next();
			
			if (folderID==null)
				folderID=0;
			
			folderID = folderID +1;			
		}
		else
		{
			folderID = folderIdRequired;
			
		}
		
		folder = new DbFolder(userID, folderID, folderName, isDropbox);
		session.save(folder);
		session.flush();
		session.clear();
		
		return folderID;
	}
	
	/**
	 * @param userID - user id
	 * @return a list of his folders (not include the root of the dropbox)
	 */
	public synchronized List<DbFolder> getUserFolders(String userID)
	{
		String queryString = "from DbFolder where userID='"+userID+"' and isDropbox=false order by folderName";
		Query query = session.createQuery(queryString);
		
		return query.list();		
	}
	
	/**
	 * @param sharedWithUserID - a user id
	 * @param folderID - a folder id
	 * @return a list of users that sharing with him this folder
	 */
	public synchronized List<String> usersSharedFolder(String sharedWithUserID, Integer folderID)
	{
		String queryString = "select userID from DbFolder where userID!='"+sharedWithUserID+"' and folderID='"+ folderID + "'";
		Query query = session.createQuery(queryString);
		
		System.out.println("userID "+ sharedWithUserID + " folderID " + folderID+ "shared with: " );
		System.out.println(query.list());
		
		return query.list();	
		
	}
	


}
